/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.capex.javaee;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Acer
 */
public class ShowParameters extends HttpServlet {

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            Enumeration<String> parameterNames = request.getParameterNames();
            Enumeration<String> headerNames = request.getHeaderNames();

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Request HTTP Parameters Sent</title>");
            out.println("</head>");
            out.println("<body>");

            out.println("<a href=\""+request.getHeader("referer")+"\">Regresar</a>");
            out.println("<h1>Parameter Recieved</h1>");
            while (parameterNames.hasMoreElements()) {
                String parameterName = parameterNames.nextElement();
                String[] parameterValues = request.getParameterValues(parameterName);
                out.println("<h3>Parameter Name: " + parameterName + "</h3>");
                for (String temp : parameterValues) {
                    
                    out.println("<p>Parameter Value: " + temp + "</p><br/>");
                }
            }
            out.println("------------------------------------------------------------------------------------");
            out.println("<h1>Header Recieved</h1>");
            while (headerNames.hasMoreElements()) {
                String headerName = headerNames.nextElement();
                Enumeration<String> headerValues = request.getHeaders(headerName);
                out.println("<h3>Parameter Name: " + headerName + "</h3>");
                while (headerValues.hasMoreElements()) {
                    String temp = headerValues.nextElement();
                    out.println("<p>Parameter Value: " + temp + "</p><br/>");
                }
            }

            out.println("</body>");
            out.println("</html>");

        } finally {
            out.close();
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
